package com.yangjie.web.jsf.user;

import java.io.IOException;
import java.util.List;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.yangjie.user.domain.User;
import com.yangjie.user.service.UserService;
import com.yangjie.web.jsf.viewcontroller.BaseBean;



@Component
@Scope(value = WebApplicationContext.SCOPE_REQUEST)
public class UserBean extends BaseBean{
	Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	@Qualifier("userService")
	private UserService userService = null;
//	private List<User> users = null;
	
	UserDataModel userDataModel = null;
	List<User> userList = null;
	private int userIdForDelete = 0;
	private User user = new User();

	private User[] selectedUsers = null;	
	
	
	
	public int getUserIdForDelete() {
		return userIdForDelete;
	}

	public void setUserIdForDelete(int userIdForDelete) {
		this.userIdForDelete = userIdForDelete;
	}




	public User[] getSelectedUsers() {
		return selectedUsers;
	}

	public void setSelectedUsers(User[] selectedUsers) {
		this.selectedUsers = selectedUsers;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<User> getUsers() {
		logger.debug("getUsers........" + this);
		userList = userService.findAllNonAdminUsers(); 
		return userList;
	}
	
	public UserDataModel getUserDataModel() {
		if(userDataModel == null) {
			userDataModel = new UserDataModel(getUsers());
		}
		return  userDataModel;
	}
	
	public void deleteSingle() {
//		userService.deleteById(userIdForDelete);
		userService.delete(user);
		userDataModel = new UserDataModel(getUsers());
	}
	
	public String update() {
		try {
		userService.update(user);
		
		return "list";
		} catch(Exception e) {
			setException(e.getMessage());
			return null;
		}
	}
	public String edit() {
		 return "/faces/user/edit";
	}
	public void delete() {
		try {
			for(User user: selectedUsers) {
				userService.delete(user);				
			}
			userDataModel = new UserDataModel(getUsers());
			logger.debug("end of delete");
		} catch(Exception e) {
			setException(e.getMessage());
		}
	}
	public String create() {
		try {		
			logger.debug("actual class:" + userService.getClass().getName());
			userService.create(user);
			return "list.xhtml";
	//		return "/faces/user/list.xhtml";

		} 
		catch(Exception e) {
			e.printStackTrace();
			setException(e.getMessage());
			return null;
		}

	}
	
	public static void main(String[] args) {
		
	}
	
}
